﻿Imports System.IO
Imports System.Text

Module Module1

    Public TXT As String = "d:\test.txt"
    Public HTM As String = "d:\test.htm"

    Public STARTQ As Char = Chr(147)
    Public ENDQ As Char = Chr(148)

    Public STARTQ2 As Char = Chr(145)
    Public ENDQ2 As Char = Chr(146)

    Public _s As String
    Public _i As Integer = 0

    Sub Main()
        _s = IO.File.ReadAllText(TXT, Text.Encoding.Default)
        If IO.File.Exists(HTM) Then IO.File.Delete(HTM)

        Dim s As String = Nothing
        Dim sb As StringBuilder = Nothing
        Dim sb2 As StringBuilder = Nothing
        While Readline(s)
            If s.Length = 0 Then Continue While
            If Not IsNothing(sb) Then
                If s.EndsWith(ENDQ) OrElse s.EndsWith("""") OrElse s.EndsWith(STARTQ) Then
                    sb.Append(s)
                    WriteQuote(sb.ToString)
                    sb = Nothing
                Else
                    sb.Append(" ").Append(s)
                End If
                Continue While
            End If

            'Heading 3
            If s.Contains(")") Then
                WriteH3(s)
                Continue While
            End If

            'Heading 1, 2
            If s.Contains(".") AndAlso s.IndexOf(".") < 5 Then
                Dim dot As Integer = s.IndexOf(".")
                Dim major As String = s.Substring(0, dot).Trim
                Dim space As Integer = s.IndexOf(" ", dot)
                Dim minor As String = String.Empty
                If space <> -1 Then minor = s.Substring(dot + 1, space - dot).Trim()
                Dim text As String = s.Substring(space + 1).Trim

                If minor.Length = 0 Then
                    WriteH1(major, text)
                Else
                    WriteH2(major, minor, text)
                End If
                Continue While
            End If

            If s.ToLower.Contains("key verse") Then
                WriteKeyVerse(s)
                Continue While
            End If
            If s.ToLower.Contains("key principle") Then
                WriteKeyVerse(s)
                Continue While
            End If
            If s.ToLower.Contains("insight") Then
                WriteInsights(s)
                Continue While
            End If

            If s.StartsWith(STARTQ) OrElse s.StartsWith("""") OrElse s.StartsWith(ENDQ) Then
                If s.Trim.EndsWith(ENDQ) OrElse s.EndsWith("""") OrElse s.EndsWith(STARTQ) Then
                    WriteQuote(s)
                Else
                    sb = New StringBuilder(s)
                End If
                Continue While
            End If



            If s.StartsWith("-") Then
                sb2 = New StringBuilder()
                sb2.AppendLine("<ul>")
                sb2.Append(" <li>")
                Bullet(sb2, s.Substring(1))
                While Readline(s)
                    If s.IndexOf(")") <> -1 AndAlso s.IndexOf(")") < 5 Then
                        sb2.AppendLine("</li>").AppendLine("</ul>")
                        WriteLine(sb2.ToString)
                        sb2 = Nothing
                        WriteH3(s)
                        Exit While
                    End If

                    If s.Length = 0 Then 'Finshed when hit a blank line
                        sb2.AppendLine("</li>").AppendLine("</ul>")
                        WriteLine(sb2.ToString)
                        sb2 = Nothing
                        Exit While
                    ElseIf s.StartsWith("-") Then 'New bullet point
                        sb2.AppendLine("</li>").Append(" <li>")
                        Bullet(sb2, s.Substring(1))
                    Else
                        sb2.Append(" ").Append(s) 'or wrap-around bullet line
                    End If
                End While

                Continue While
            End If

            If s.ToLower.StartsWith("example") Then
                WriteKeyVerse(s)
                Continue While
            End If

            'WritePara(s)
            WriteInsights(s)
        End While

    End Sub

    Private Sub Bullet(sb As StringBuilder, s As String)
        If s.Trim.StartsWith(STARTQ2) AndAlso s.Contains(ENDQ2) Then
            Dim i As Integer = s.IndexOf(ENDQ2)
            sb.Append("<span class=""bold"">")
            sb.Append(s.Substring(0, i + 1))
            sb.Append("</span>")
            sb.Append(s.Substring(i + 1))
        Else
            sb.Append(s)
        End If
    End Sub

    Private Function Readline(ByRef line As String) As Boolean
        line = String.Empty
        If _i > _s.Length Then Return False
        Dim nextLine As Integer = _s.IndexOf(vbCrLf, _i)
        If -1 = nextLine Then Return False
        line = _s.Substring(_i, nextLine - _i).Trim
        _i = nextLine + 1
        Return True
    End Function

    Private Sub WriteH1(major As String, text As String)
        WriteLine("<h1>", major, ". ", text, "</h1>")
    End Sub
    Private Sub WriteH2(major As String, minor As String, text As String)
        WriteLine("<h2>", major, ".", minor, " ", text, "</h2>")
    End Sub
    Private Sub WriteH3(text As String)
        WriteLine("<h3>", text, "</h3>")
    End Sub

    Private Sub WriteKeyVerse(text As String)
        WriteLine("<p class=""keyverse"">", text, "</p>")
    End Sub
    Private Sub WriteQuote(text As String)
        WriteLine("<p class=""quote"">", text, "</p>")
    End Sub
    Private Sub WriteInsights(text As String)
        WriteLine("<p class=""insights"">", text, "</p>")
    End Sub
    Private Sub WritePara(text As String)
        WriteLine("<p>", text, "</p>")
    End Sub
    Private Sub WriteLine(ParamArray line As Object())
        IO.File.AppendAllText(HTM, String.Concat(line) & vbCrLf, Text.Encoding.Default)
    End Sub

End Module
